package com.guxingke.typecho4j.widget.provider.user;

import javax.servlet.http.HttpSession;

import com.guxingke.typecho4j.Typecho4jException;
import com.guxingke.typecho4j.jfinal.model.User;
import com.guxingke.typecho4j.util.StringUtil;
import com.guxingke.typecho4j.widget.Widget;
import com.guxingke.typecho4j.widget.WidgetProvider;
import com.jfinal.core.Controller;

/**
 * 已登录用户访问登录页直接跳转至后台
 * 
 * @author guxingke
 *
 */
public class LoginPageWidget extends Widget implements WidgetProvider {

	@Override
	public void excute(Controller controller) throws Typecho4jException {
		HttpSession session = controller.getSession();
		User user = (User) session.getAttribute("user");

		if (user != null && "administrator".equals(user.get("group"))) {
			controller.redirect("/admin");
		} else {
			Integer errorCode = controller
					.getParaToInt("errorCode");
			if (errorCode != null) {
				controller.setAttr(
						"error",
						StringUtil.getErrorInfoByErrorCode(errorCode));
			}
			controller.render("/admin/login.jsp");
		}

	}

	@Override
	public void refresh() {
	}

}
